/*
 * 创建日历数据
 * 
 * 本文件只会生成日历组建所需要的数据，数据的结构根据页面DOM结构来相应组成，不涉及页面DOM操作
 * 
 * init函数：初始化函数，接收参数对象其参数属性分别为：year/month/day/lang 四个, 
 * 		         如任何参数都不传的话，默认获取设备当前时间，文字类型为中文
 * 
 * 调用传参：
 *		(new MobileDate()).init({
 *			year: 2015,
 * 			month: 11,
 * 			day: 13,
 * 			lang: 'cn'
 *		})
 * 
 * 	返回值：返回一个对象数据集合 -> result对象
 * 
 */
!(function(){
	
	function MobileDate() {
		this.nowDate = new Date();
		this.initDataYear = 2015;
		this.defaultDate = {
			year     : this.nowDate.getFullYear(),
			month    : this.nowDate.getMonth() + 1,
			day      : this.nowDate.getDate(),
			week     : this.nowDate.getDay(),
			lang     : 'cn',
			oldYear  : this.nowDate.getFullYear(),
			oldMonth : this.nowDate.getMonth() + 1,
			today    : this.nowDate.getDate()
		};
	}
	
	// 数据替换，用于传入的数据覆盖默认生成的数据
	function extend(target, source) {
		
		for(var key in source){
	        target[key] = source[key]
	    }
	    return target;
	}

	MobileDate.prototype = {
		'setDate' : function(opts) {
			var _this = this,
				_changeDate,
				customDate;
				
			_changeDate = new Date(opts.year, opts.month - 1, opts.day);
			
			customDate = {
				year: _changeDate.getFullYear(),
				month: _changeDate.getMonth() + 1,
				day: _changeDate.getDate(),
				week: _changeDate.getDay(),
				lang: opts.lang
			}

			return customDate;
		},
		'init' : function(opts) {
			var _this = this,
				_opt = extend( _this.defaultDate, opts);

			this.customDate = extend( _this.defaultDate, _this.setDate(_opt));

			if ((_this.customDate.year - _this.initDataYear) < 0) {
				console.error('\'year\' is not less than the current date');
				return false;
			}

			var _prevMonthDay = _this.getDay(_this.customDate.year, _this.customDate.month - 1),
				_nowMonthDay = _this.getDay(_this.customDate.year, _this.customDate.month),
				_firstWeek = new Date(_this.customDate.year, _this.customDate.month - 1, 1).getDay(),
				_lang = _this.setLang(_this.customDate.lang);
			
			var result = {
				year      : _this.customDate.year,
				yearText  : _this.customDate.lang === 'cn' ? _this.customDate.year + '年' : _this.customDate.year,
				yearArr   : _this.getYear(_this.customDate.oldYear),
				month     : _this.customDate.month,
				monthText : _this.getMonth(_this.customDate.month),
				monthArr  : _lang.month,
				day       : _this.customDate.day,
				dayTexts  : _lang.day,
				dayArr    : _this.getDayList(_prevMonthDay, _nowMonthDay, _firstWeek, _this.customDate.day, _this.customDate.today),
				week      : _this.customDate.week,
				weekStr   : _this.getWeek(_this.customDate.week),
				oldYear   : _this.customDate.oldYear,
				oldMonth  : _this.customDate.oldMonth,
				today     : _this.customDate.today
			};

			return result;
		},
		'setLang' : function(lang) {
			var result_obj;
			
			switch (lang) {
				case 'en' :
					result_obj = {
						month : [
							'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
						],
						week : [
							'Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'
						],
						day : [
							'Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'
						]
					}
					break;
				case 'cn' :
					result_obj = {
						month : [
							'01月', '02月', '03月', '04月', '05月', '06月', '07月', '08月', '09月', '10月', '11月', '12月'
						],
						week : [
							'星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'
						],
						day : [
							'日', '一', '二', '三', '四', '五', '六'
						]
					}
					break;
			}

			return result_obj;
		},
		'getWeek' : function(week) {
			var _this = this,
				week_str = _this.setLang(_this.customDate.lang).week,
				result_week_str;
			
			for (var i = 0; i < week_str.length; i++) {
				if (i === week) {
					result_week_str =  week_str[i]
				}
			}
			
			return result_week_str;
		},
		'getMonth' : function(month) {
			var _this = this,
				month_str = _this.setLang(_this.defaultDate.lang).month,
				result_month_str;
			
			for (var i = 0; i < month_str.length; i++) {
				if (i === (month - 1)) {
					result_month_str = month_str[i]
				}
			}
			
			return result_month_str;
		},
		'getDay' : function(year, month) {
			var _year = parseInt(year, 10),
				_month = parseInt(month, 10),
				_day = 31,
				_smallMonth = [4,6,9,11],
				_isLeapYear = this.isLeapYear(_year);

			switch (true) {
				case _smallMonth.indexOf(_month) != -1 : 
					_day = 30;
					break;
				case _isLeapYear && _month === 2 :
					_day = 29;
					break;
				case !_isLeapYear && _month === 2 :
					_day = 28;
					break;
			}

			return _day;
		},
		'getDayList' : function(prevMonthDay, selecMonthDay, week, day, today) {
			var _allDayLen = 42,
				_allDayArr = [],
				_resultDayArr = [],
				_singleDayArr = [],
				_singleDayObj = {},
				_lastNum = _allDayLen - week - selecMonthDay;

			if (week > 0) {
				for (var i = week - 1; i >= 0 ; i--) {
					_singleDayObj = {
						dayNum : prevMonthDay - i,
						isSelect : false,
						isToday: false
					}
					_allDayArr.push(_singleDayObj);
				}
			}
			
			for (var i = 0; i < selecMonthDay; i++) {
				_singleDayObj = {
					dayNum : i + 1,
					isSelect : true,
					isToday: i + 1 === day ? true : false
				}
				
				_allDayArr.push(_singleDayObj);
			}
			
			for (var i = 0; i < _lastNum; i++) {
				_singleDayObj = {
					dayNum : i + 1,
					isSelect : false,
					isToday: false
				}
				_allDayArr.push(_singleDayObj);
			}
			
			for (var i = 0, len = _allDayArr.length / 7; i < len; i++) {
				_singleDayArr = _allDayArr.splice(0,7);
				_resultDayArr.push(_singleDayArr);
			}

			return _resultDayArr;
		},
		'isLeapYear' : function (year) {
			var result = false;
			
			if (year % 4 == 0 && ((year % 100 != 0) || (year % 400 == 0))) {
			  result = true;
			}
			
			return result;
		},
		'getYear' : function (year) {
			var _differ = year - this.initDataYear + 1,
				_resultYearArr = [];
				
			for (var i = 0; i < _differ; i++) {
				_resultYearArr.push(this.initDataYear + i);
			}
			
			return _resultYearArr;
		}
	}
	
	window.MobileDate = MobileDate;
	
})();
